import BottomTab from '@/components/BottomTab'
import Loading from '@/components/Loading'
import { useToast } from '@/hooks/toast'
import EventBus from '@/utils/EventBus'
import React, { Suspense, useEffect } from 'react'
import { Outlet, useNavigate } from 'react-router-dom'
export default function App() {
    const toast = useToast()
    const nav = useNavigate()
    // 在这 写 一个 错误捕获的订阅
    EventBus.on('errCatch', (data: any) => {
        toast(data.message, "error")
        // 如果是登录已经失效
        if (data.errCode === 1002) {
            // 清空token
            localStorage.removeItem('token')
            // 跳转到 login
            nav('/login')
        }

    })

    // 在这进行路由拦截
    // 如果没登录，就跳转到login
    let token = localStorage.getItem('token')
    useEffect(() => {
        if (!token) {
            // 跳转到登录
            nav('/login')
        }
    })
    if (token) {
        return (
            <div>
                <Suspense fallback={<Loading />}>
                    <Outlet />
                </Suspense>

                <BottomTab />
            </div>
        )
    } else {
        return <></>
    }

}
